package com.iptv.grape.bu;

import android.app.Activity;
import android.content.Context;

import com.google.gson.Gson;
import com.iptv.grape.TestActivity;
import com.iptv.grape.domain.ChannelHis;
import com.iptv.grape.net.Entity.ChannelData;
import com.iptv.grape.net.Entity.ConfigData;
import com.iptv.grape.net.NetworkService;
import com.iptv.grape.util.ReadJsonFile;

import java.util.List;

public class AppConfig {

    // 心跳包时间间隔(秒)
    private int heartBeatTime = 10;
    // Vod试看时间(秒)
    private int vodTryTime = 600;
    // 3A认证通过后下次认证的时间(小时)
    private int accreditationSuccess = 24;
    // 3A认证失败后下次认证的时间(小时)
    private int authenticationFailure = 1;
    // 时移全局开关
    private boolean canTimeLive = true;
    // 回看的全局开关
    private boolean canBackLive = true;
    // 直播的底部推荐的全局开关
    private boolean canShowLiveRecommend = true;
    // 时移最长的时间(秒)
    private int timeLiveMaxVal = 7200;
    // 时移进度条步长
    private int timeLiveStepVal = 60;
    // 频道列表缓存版本号
    private String channelVer = "v1";
    // 频道类别列表缓存版本号
    private String channelTypeVer = "v1";
    // 当天节目单列表缓存版本号
    private String playBillVer = "v1";
    // 机顶盒配置列表缓存版本号
    private String configVar = "v1";
    // 机顶盒点播暂停图片缓存版本号
    private String adImageVar = "v1";
    // 开机启动图版本号
    private String imgVersion = "v1";
    // H5网页的版本号
    private String epgVer = "v1";
    // 自动关闭菜单的时间(秒)
    private int autoCloseMenuTime = 10;

    // 开机图片显示时间
    private int runImageShowTime = 6;
    // EPG网页首地址
    private String webViewUrl = "http://117.158.207.135:8899";
    // Epg详情页基地址
    private String epgDetailUrl =  "http://117.158.207.135:8899/index.html#/details/";

    // UI控件延时获得焦点(毫秒)
    private int delayedTimeVal = 100;

    // 预约对话框宽度
    private int channelBillDlgWidth = 800;

    // 回看模式下进度条的步长（秒）
    private int backLiveModeKeyProgressIncrement = 20;
    // 点播模式下进度条的步长（秒）
    private int vodModeKeyProgressIncrement = 20;

    public boolean isCanShowLiveRecommend() {
        return canShowLiveRecommend;
    }

    public void setCanShowLiveRecommend(boolean canShowLiveRecommend) {
        this.canShowLiveRecommend = canShowLiveRecommend;
    }

    public int getVodModeKeyProgressIncrement() {
        return vodModeKeyProgressIncrement;
    }

    public void setVodModeKeyProgressIncrement(int vodModeKeyProgressIncrement) {
        this.vodModeKeyProgressIncrement = vodModeKeyProgressIncrement;
    }

    public int getBackLiveModeKeyProgressIncrement() {
        return backLiveModeKeyProgressIncrement;
    }

    public void setBackLiveModeKeyProgressIncrement(int backLiveModeKeyProgressIncrement) {
        this.backLiveModeKeyProgressIncrement = backLiveModeKeyProgressIncrement;
    }

    private void initCheck(){
        if(this.adImageVar == null){
            this.adImageVar = "v1";
        }
        if(this.imgVersion == null){
            this.imgVersion = "v1";
        }
    }

    public String getEpgVer() {
        return epgVer;
    }

    public void setEpgVer(String epgVer) {
        this.epgVer = epgVer;
    }

    public String getAdImageVar() {
        return adImageVar;
    }

    public void setAdImageVar(String adImageVar) {
        this.adImageVar = adImageVar;
    }

    public int getDelayedTimeVal() {
        return delayedTimeVal;
    }

    public String getEpgDetailUrl() {
        return epgDetailUrl;
    }

    public String getWebViewUrl() {
        return webViewUrl;
    }

    public int getRunImageShowTime() {
        return runImageShowTime;
    }

    public int getChannelBillDlgWidth() {
        return channelBillDlgWidth;
    }

    public int getAutoCloseMenuTime() {
        return autoCloseMenuTime;
    }

    public String getChannelVer() {
        return channelVer;
    }
    public void setChannelVer(String channelVer) {
        this.channelVer = channelVer;
    }


    public String getChannelTypeVer() {
        return channelTypeVer;
    }

    public String getPlayBillVer() {
        return playBillVer;
    }

    public void setPlayBillVer(String playBillVer) {
        this.playBillVer = playBillVer;
    }

    public String getConfigVar() {
        return configVar;
    }

    public void setChannelTypeVer(String channelTypeVer) {
        this.channelTypeVer = channelTypeVer;
    }

    public void setConfigVar(String configVar) {
        this.configVar = configVar;
    }

    public String getImgVersion() {
        return imgVersion;
    }

    public void setImgVersion(String imgVersion){
        this.imgVersion = imgVersion;
    }

    public int getHeartBeatTime() {
        return heartBeatTime;
    }

    public int getVodTryTime() {
        return vodTryTime;
    }

    public int getAccreditationSuccess() {
        return accreditationSuccess;
    }

    public int getAuthenticationFailure() {
        return authenticationFailure;
    }

    public boolean isCanTimeLive() {
        return canTimeLive;
    }

    public boolean isCanBackLive() {
        return canBackLive;
    }

    public int getTimeLiveMaxVal() {
        return timeLiveMaxVal;
    }

    public int getTimeLiveStepVal() {
        return timeLiveStepVal;
    }

    public static AppConfig getM_AppConfig() {
        return m_AppConfig;
    }

    private static AppConfig m_AppConfig = null;

    // 获得全局配置的实例
    public static AppConfig getAppConfig(){
        return m_AppConfig;
    }

    // 初始化
    public static void init(Context context){
        m_AppConfig = unSerializeToFile(context);
    }


    public boolean syncData(){
        try{
            List<ConfigData> resultList = NetworkService.getInstance().getConfigListAsync();
            if(resultList == null || resultList.size() == 0){
                return false;
            }
            for(int i = 0; i < resultList.size(); i++){
                ConfigData configData = resultList.get(i);
                syncDataHelp(configData);
            }
            this.serializeToFile();
            return true;
        }
        catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    private void syncDataHelp(ConfigData configData){
        try {
            switch (configData.getKey()) {
                case "timeLiveStepVal":
                    this.timeLiveStepVal = safeToint(configData.getVal(),this.timeLiveStepVal);
                    break;
                case "authenticationFailur":
                    this.authenticationFailure = safeToint(configData.getVal(),this.authenticationFailure);
                    break;
                case "timeLiveMaxVal":
                    this.timeLiveMaxVal = safeToint(configData.getVal(),this.timeLiveMaxVal );
                    break;
                case "runImageShowTime":
                    this.runImageShowTime = safeToint(configData.getVal(),this.runImageShowTime);
                    break;
                case "canBackLive":
                    this.canBackLive = configData.getVal().equals("true");
                    break;
                case "canShowLiveRecommend":
                    this.canShowLiveRecommend = configData.getVal().equals("true");
                    break;
                case "accreditationSuccess":
                    this.accreditationSuccess = safeToint(configData.getVal(),this.accreditationSuccess);
                    break;
                case "heartBeatTime":
                    this.heartBeatTime = Integer.parseInt(configData.getVal());
                    break;
                case "autoCloseMenuTime":
                    this.autoCloseMenuTime = safeToint(configData.getVal(),this.autoCloseMenuTime);
                    break;
                case "canTimeLive":
                    this.canTimeLive = configData.getVal().equals("true") ;
                    break;
                case "vodTryTime":
                    this.vodTryTime = safeToint(configData.getVal(),this.vodTryTime);
                    break;
                case "webViewUrl":
                    this.webViewUrl = configData.getVal();
                    break;
                case "epgDetailUrl":
                    this.epgDetailUrl = configData.getVal();
                    break;
                case "backLiveModeKeyProgressIncrement":
                    this.backLiveModeKeyProgressIncrement = safeToint(configData.getVal(),this.backLiveModeKeyProgressIncrement);
                    break;
                case "vodModeKeyProgressIncrement":
                    this.vodModeKeyProgressIncrement = safeToint(configData.getVal(),this.vodModeKeyProgressIncrement);
                    break;
            }
        }
        catch (Exception e){

        }
    }



    private int safeToint(String val,int defVal){
        try{
            return Integer.parseInt(val);
        }
        catch (Exception e){
            return defVal;
        }
    }


    // 从文件中反序列化对象
    private static AppConfig unSerializeToFile(Context context){
        AppConfig appConfig = null;
        String configJson = null;
        if(ReadJsonFile.fileExists("config.json")) {
            configJson = ReadJsonFile.readFile("config.json");
        }
        if(configJson == null){
            String json = ReadJsonFile.readFileByAssets("stbBaseConfig.json", context);
            appConfig = new Gson().fromJson(json, AppConfig.class);
            appConfig.serializeToFile();
        }
        else {
            appConfig = new Gson().fromJson(configJson, AppConfig.class);
        }
        appConfig.initCheck();
        return appConfig;
    }

    // 保存到文件
    public void serializeToFile(){
        Gson gson = new Gson();
        String json = gson.toJson(this);
        ReadJsonFile.writeFile("config.json",json);
    }
}
